package main

import (
    // "time"
    // "image/color"
    // "fyne.io/fyne/v2/canvas"
    // "fyne.io/fyne/v2/layout"    
    "fmt"
    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/widget"
    "gorm.io/driver/sqlite"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/data/binding"
    "fyne.io/fyne/v2/theme"    
    "gorm.io/gorm"
    "os"
    "log"
)
type Todo struct {
  ID    uint   `json:"id"`
  Text  string `json:"text"`
  Completed    bool `json:"completed"`
}
type Tabler interface {
  TableName() string
}

// TableName overrides the table name used by User to `profiles`
func (Todo) TableName() string {
  return "todos_todo"
}
var db *gorm.DB
var err error
// func main0() {
//     data := binding.BindStringList(
//         &[]string{"Item 1", "Item 2", "Item 3"},
//     )
//     db, err = gorm.Open(sqlite.Open("./data.sqlite"), &gorm.Config{})
//     //使用mysql, gorm.Open(“mysql”, “user:pwd@tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local”)
//     if err != nil {
//         log.Fatal("db connect error")
//     }
//     get_todos()
//     // fmt.Println(data2)
//     a := app.New()
//     w := a.NewWindow("Todo")
//     input1:=widget.NewEntry()
//     createButton:=widget.NewButton("new Todo",func(){
//         db.Create(&Todo{Text: input1.Text, Completed:false})
//     })
//     getButton:=widget.NewButton("refresh",func(){

//     })
//     list := widget.NewListWithData(data,
//         func() fyne.CanvasObject {
//             return widget.NewLabel("template")
//         },
//         func(i binding.DataItem, o fyne.CanvasObject) {
//             o.(*widget.Label).Bind(i.(binding.String))
//         })

//     add := widget.NewButton("Append", func() {
//         val := fmt.Sprintf("Item %d", data.Length()+1)
//         data.Append(val)
//     })
//     box:=container.NewVBox(input1,createButton,getButton,add,list)
//     w.SetContent(box) 
//     w.Show()

//     a.Run()
// }
func get_todos() []Todo{
    var users []Todo
    db.Find(&users)
    return users
}
func put_todos(text string,completed bool) Todo {
    var user Todo
    user.Text=text
    user.Completed=completed
    db.Save(&user)
    return user
}
func createTodo() {
    db.Create(&Todo{Text: "D42", Completed:false})
}
func main() {
    fmt.Println("begin")
    db, err = gorm.Open(sqlite.Open("./data.sqlite"), &gorm.Config{})
    //使用mysql, gorm.Open(“mysql”, “user:pwd@tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local”)
    if err != nil {
        log.Fatal("db connect error")
    }
    data1:=get_todos();
    var selected Todo;
    fmt.Println(data1);
    myApp := app.New()
    myApp.Settings().SetTheme(theme.DarkTheme())
    myWindow := myApp.NewWindow("List Data")

    data := binding.BindStringList(
        &[]string{},
    )
    for i:=0; i < len(data1); i++ {
        data.Append(data1[i].Text)
    }
    refresh:=func(){
        data1=get_todos()
        fmt.Println(data1)
        data.Set([]string{})
        for i:=0; i < len(data1); i++ {
            data.Append(data1[i].Text)
        }
    }
    list := widget.NewListWithData(data,
        func() fyne.CanvasObject {
            return widget.NewLabel("template")
        },
        func(i binding.DataItem, o fyne.CanvasObject) {
            o.(*widget.Label).Bind(i.(binding.String))

        })
    list.OnSelected = func(id widget.ListItemID) {
        fmt.Println("on selected")
        fmt.Println(id)
        fmt.Println(len(data1))
        selected=data1[id]
    }
    removeButton := widget.NewButton("remove", func() {
        fmt.Println(selected)
        fmt.Println("remove");
        db.Delete(&selected)
        //refresh
        refresh()
    })
    label := widget.NewLabel("text:")
    input1:=widget.NewEntry()
    box0:=container.NewHBox(label,input1)
    createButton:=widget.NewButton("new Todo",func(){
        //db.Create(&Todo{Text: input1.Text, Completed:false})
        put_todos(input1.Text,false)
        //refresh
        refresh()

    })
    getButton:=widget.NewButton("refresh",func(){
        refresh()
    })
    exitButton:=widget.NewButton("exit",func(){
        os.Exit(0)
    })
    box1:=container.NewVBox(box0,createButton)
    box2:=container.NewVBox(removeButton,getButton,exitButton)
    myWindow.SetContent(container.NewBorder(box1, box2, nil, nil, list))
    //top,down,left,right,middle
    myWindow.ShowAndRun()
}
// func main2() {
//     myApp := app.New()
//     myWindow := myApp.NewWindow("Box Layout")

//     text1 := canvas.NewText("Hello", color.White)
//     text2 := canvas.NewText("There", color.White)
//     text3 := canvas.NewText("(right)", color.White)
//     content := container.New(layout.NewHBoxLayout(), text1, text2, layout.NewSpacer(), text3)

//     text4 := canvas.NewText("centered", color.White)
//     centered := container.New(layout.NewHBoxLayout(), layout.NewSpacer(), text4, layout.NewSpacer())
//     myWindow.SetContent(container.New(layout.NewVBoxLayout(), content, centered))
//     myWindow.ShowAndRun()
// }
// func main3() {
//     myApp := app.New()
//     myWindow := myApp.NewWindow("Grid Layout")

//     text1 := canvas.NewText("1", color.White)
//     text2 := canvas.NewText("2", color.White)
//     text3 := canvas.NewText("3", color.White)
//     grid := container.New(layout.NewGridLayout(2), text1, text2, text3)
//     myWindow.SetContent(grid)
//     myWindow.ShowAndRun()
// }
